class ykSet extends Set {
  union(...sets) {
    return ykSet.union(this, ...sets);
  }
  intersection(...sets) {
    return ykSet.intersection(this, ...sets);
  }
  difference(set) {
    return ykSet.difference(this, set);
  }
  symmetricDifference(set) {
    return ykSet.symmetricDifference(this, set);
  }
  powerSet() {
    return ykSet.powerSet(this);
  }

  // 多个集合的并集
  static union(a, ...bSets) {
    const unionSet = new ykSet(a);

    for (const b of bSets) {
      for (const bValue of b) {
        unionSet.add(bValue);
      }
    }

    return unionSet;
  }

  // 多个集合的交集
  static intersection(a, ...bSets) {
    const intersectionSet = new ykSet(a);
    for (const aValue of intersectionSet) {
      for (const b of bSets) {
        if (!b.has(aValue)) {
          intersectionSet.delete(aValue);
        }
      }
    }
    return intersectionSet;
  }

  // 差集
  static difference(a, b) {
    const differenceSet = new ykSet(a);
    for (const bValue of b) {
      if (a.has(bValue)) {
        differenceSet.delete(bValue);
      }
    }
    return differenceSet;
  }

  // 对称差集
  static symmetricDifference(a, b) {
    return a.union(b).difference(a.intersection(b));
  }

  // 笛卡尔积
  static cartesianProduct(a, b) {
    const cartesianProductSet = new ykSet();
    for (const aValue of a) {
      for (const bValue of b) {
        cartesianProductSet.add([aValue, bValue]);
      }
    }
    return cartesianProductSet;
  }

  // 幂集
  static powerSet(a) {
    const powerSet = new ykSet().add(new ykSet());
    for (const aValue of a) {
      for (const set of new ykSet(powerSet)) {
        powerSet.add(new ykSet(set).add(aValue));
      }
    }
    return powerSet;
  }
}
 